<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Script condition | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'condition-script.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/condition-script.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/condition-script.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/condition-script.html" rel="nofollow" target="_blank">../en/condition-script.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="xpack-alerting.html">Alerting on cluster and index events</a></span>
»
<span class="breadcrumb-link"><a href="condition.html">Conditions</a></span>
»
<span class="breadcrumb-node">Script condition</span>
</div>
<div class="navheader">
<span class="prev">
<a href="condition-array-compare.html">« Array compare condition</a>
</span>
<span class="next">
<a href="actions.html">Actions »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="condition-script"></a>Script condition<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/condition/script.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<p>A watch <a class="xref" href="condition.html" title="Conditions">condition</a> that evaluates a script. The default scripting
language is <code class="literal">painless</code>. You can use any of the scripting languages supported by
Elasticsearch as long as the language supports evaluating expressions to Boolean
values. Note that the <code class="literal">mustache</code> and <code class="literal">expression</code> languages are too limited to be
used by this condition. For more information, see <a class="xref" href="modules-scripting.html" title="Scripting">Scripting</a>.</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_using_a_script_condition"></a>Using a script condition<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/condition/script.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The following snippet configures an inline <code class="literal">script</code> condition that always returns
<code class="literal">true</code>:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"condition" : {
  "script" : "return true"
}</pre>
</div>
<p>This example defines a script as a simple string. This format is actually a
shortcut for defining an <a class="xref" href="condition-script.html#condition-script-inline" title="Inline scripts">inline</a> script. The
formal definition of a script is an object that specifies the script type and
optional language and parameter values. If the <code class="literal">lang</code> attribute is omitted, the
language defaults to <code class="literal">painless</code>. Elasticsearch supports two types of scripts,
<a class="xref" href="condition-script.html#condition-script-inline" title="Inline scripts">inline</a> and <a class="xref" href="condition-script.html#condition-script-stored" title="Stored scripts">stored</a>.</p>
<p>For example, the following snippet shows a formal definition of an <code class="literal">inline</code>
script that explicitly specifies the language and defines a single script
parameter, <code class="literal">result</code>:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"condition" : {
  "script" : {
    "source" : "return params.result",
    "lang" : "painless",
    "params" : {
      "result" : true
    }
  }
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="condition-script-inline"></a>Inline scripts<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/condition/script.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Inline scripts are scripts that are defined in the condition itself. The
following snippet shows the formal configuration of a simple painless script that
always returns <code class="literal">true</code>.</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"condition" : {
  "script" : {
    "source" : "return true"
  }
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="condition-script-stored"></a>Stored scripts<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/condition/script.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Stored scripts refer to scripts that were
<a class="xref" href="modules-scripting-using.html#modules-scripting-stored-scripts" title="Stored scripts">stored</a> in Elasticsearch. The following
snippet shows how to refer to a script by its <code class="literal">id</code>:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"condition" : {
  "script" : {
    "id" : "my_script"
  }
}</pre>
</div>
<p>As with <a class="xref" href="condition-script.html#condition-script-inline" title="Inline scripts">inline</a> scripts, you can also specify the
script language and parameters:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">"condition" : {
  "script" : {
    "id" : "my_script",
    "lang" : "javascript",
    "params" : { "color" : "red" }
  }
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="accessing-watch-payload"></a>Accessing the watch payload<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/condition/script.asciidoc">edit</a>
</h3>
</div></div></div>
<p>A script can access the current watch execution context, including the payload
data, as well as any parameters passed in through the condition definition.</p>
<p>For example, the following snippet defines a watch that uses a
<a class="xref" href="input-search.html" title="Search input"><code class="literal">search</code> input</a> and uses a <code class="literal">script</code> condition to check if the
number of hits is above a specified threshold:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "input" : {
    "search" : {
      "indices" : "log-events",
      "body" : {
        "size" : 0,
        "query" : { "match" : { "status" : "error" } }
      }
    }
  },
  "condition" : {
    "script" : {
      "source" : "return ctx.payload.hits.total &gt; params.threshold",
      "params" : {
        "threshold" : 5
      }
    }
  }
}</pre>
</div>
<p>When you’re using a scripted condition to evaluate an Elasticsearch response,
keep in mind that the fields in the response are no longer in their native data
types. For example, the <code class="literal">@timestamp</code> in the response is a string, rather than a
<code class="literal">DateTime</code>. To compare the response <code class="literal">@timestamp</code> against the <code class="literal">ctx.execution_time</code>,
you need to parse the <code class="literal">@timestamp</code> string into a <code class="literal">DateTime</code>. For example:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">org.elasticsearch.common.joda.time.DateTime.parse(@timestamp)</pre>
</div>
<p>You can reference the following variables in the watch context:</p>
<div class="informaltable">
<table border="1" cellpadding="4px">
<colgroup>
<col class="col_1">
<col class="col_2">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">Name</th>
<th align="left" valign="top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.watch_id</code></p></td>
<td align="left" valign="top"><p>The id of the watch that is currently executing.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.execution_time</code></p></td>
<td align="left" valign="top"><p>The time execution of this watch started.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.trigger.triggered_time</code></p></td>
<td align="left" valign="top"><p>The time this watch was triggered.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.trigger.scheduled_time</code></p></td>
<td align="left" valign="top"><p>The time this watch was supposed to be triggered.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.metadata.*</code></p></td>
<td align="left" valign="top"><p>Any metadata associated with the watch.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.payload.*</code></p></td>
<td align="left" valign="top"><p>The payload data loaded by the watch’s input.</p></td>
</tr>
</tbody>
</table>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="condition-array-compare.html">« Array compare condition</a>
</span>
<span class="next">
<a href="actions.html">Actions »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>